﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       CommunicateFactory
- 机器名称：       YTE
- 命名空间：       LD.Common.Communicate
- 文 件 名：       CommunicateFactory
- 创建时间：       2022/7/25 14:16:56
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.PLC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace LD.Host
{
    public class CommunicateFactory
    {
        private string fileName = Path.Combine(Appbus.BasePath, "Config\\hd.config");
        private static CommunicateFactory instance;
        private static object obj = new object();
        private CommunicateFactory()
        {

        }
        public static CommunicateFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (obj)
                    {
                        if (instance == null)
                        {
                            instance = new CommunicateFactory();
                        }
                    }
                }
                return instance;
            }
        }
        public Dictionary<string, SerialHelper> Com { get; private set; } = new Dictionary<string, SerialHelper>();
        public Dictionary<string, SocketHelper> TcpIp { get; private set; } = new Dictionary<string, SocketHelper>();
        public Dictionary<string, ModbusCom> ModbusRtus { get; private set; } = new Dictionary<string, ModbusCom>();
        public void Init()
        {
            GetList = JsonSerializeHelper.Load<List<TB_HD>>(fileName);
            if (GetList == null) return;
            foreach (var item in GetList)
            {
                HDType hDType = (HDType)Enum.Parse(typeof(HDType), item.HDType + "");
                switch (hDType)
                {
                    case HDType.Com:
                        if (item.Model >= 0)
                        {
                            var plc = (PLCType)Enum.Parse(typeof(PLCType), item.Model + "");

                            if (plc == PLCType.ModbusRtu)
                            {
                                var _item = new ModbusCom();
                                _item.HDParam = item;
                                ModbusRtus.Add(item.Name, _item);
                            }
                        }
                        else
                            Com.Add(item.Name, new SerialHelper(item));

                        break;
                    case HDType.PLC:
                        PLCFactory.ActionPLC = (PLCType)Enum.Parse(typeof(PLCType), item.Model + "");
                        PLCFactory.Instance.Name = item.Name;
                        PLCFactory.Instance.HDParam = item;
                        break;
                    case HDType.TcpIp:
                        var tcp = new SocketHelper(item.Address, item.Port);
                        tcp.Name = item.Name;
                        TcpIp.Add(item.Name, tcp);
                        break;
                    default:
                        break;
                }

                Task.Delay(5).Wait();
            }

            Start();
        }
        public void Start()
        {
            foreach (var item in Com)
            {
                Task.Run(() =>
                {
                    item.Value.Start();
                });
            }
            foreach (var item in ModbusRtus)
            {
                Task.Run(() =>
                {
                    item.Value.Start();
                });
            }
            //foreach (var item in PLC)
            //{
            //    Task.Run(() =>
            //    {
            //        item.Value.Start();
            //    });
            //}
            foreach (var item in TcpIp)
            {
                Task.Run(() =>
                {
                    item.Value.Start();
                });
            }

            Task.Run(() =>
            {
                PLCFactory.Instance.Start();
            });
        }
        public void Stop()
        {
            foreach (var item in Com)
            {
                Task.Run(() =>
                {
                    item.Value.Stop();
                });
            }
            //foreach (var item in PLC)
            //{
            //    Task.Run(() =>
            //    {
            //        item.Value.Stop();
            //    });
            //}
            foreach (var item in TcpIp)
            {
                Task.Run(() =>
                {
                    item.Value.Stop();
                });
            }
            Task.Run(() =>
            {
                PLCFactory.Instance.Stop();
            });
        }
        public List<TB_HD> GetList { get; set; }

        public void Save()
        {
            JsonSerializeHelper.Save(GetList, fileName);
        }
    }
}
